﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace eComet
{
    public partial class NoParamsWin : Form
    {
        public String[] argv;
        private bool debug = false;
        Dictionary<string, string> actions = new Dictionary<string, string>();
        Dictionary<string, List<string>> files = new Dictionary<string, List<string>>();
        String fileExtensions = ".mp3,.m4a,.mp4,.wav";
        bool scanComplete = false;

        public NoParamsWin()
        {
            InitializeComponent();
        }

        public NoParamsWin(String[] argv)
        {
            InitializeComponent();
            this.argv = argv;
        }

        private void NoParamsWin_Load(object sender, EventArgs e)
        {
            Thread hilo = new Thread(cargaParams);
            hilo.Start();
        }

        public void cargaParams()
        {
            try
            {
                richTextBox1.Text = "Analizing files ...\n";
                ToxUtils tu = new ToxUtils(argv);
                String paths = tu.getPaths();
                actions = tu.getActions("/own");
                String textLabel = "";
                foreach (String llave in actions.Keys)
                {
                    switch (llave)
                    {
                        case "/ownart":
                            textLabel = "Set artist name: ";
                            break;
                        case "/ownalb":
                            textLabel = "Set album name: ";
                            break;
                        case "/ownson":
                            textLabel = "Set song name: ";
                            break;
                        case "/owngen":
                            textLabel = "Set genere type: ";
                            break;
                        default:
                            textLabel = "Set other: ";
                            break;
                    }
                }
                this.Text = textLabel;
                files = tu.getFiles(paths, fileExtensions);
                scanComplete = true;
                //ProcessFiles(files, actions);
            }
            catch (Exception e)
            {
                richTextBox1.Text = e.Message + "\n\n" + richTextBox1.Text;
            }
        }

        public bool ProcessFiles(Dictionary<string, List<string>> files, Dictionary<string, string> actions)
        {
            bool exit = false;
            if (files.Count > 0)
            {
                richTextBox1.Text = "Processing files ...\n\n";
                string salida;
                foreach (String tipo in files.Keys)
                {
                    switch (tipo)
                    {
                        default:
                            foreach (String archivo in files[tipo])
                            {
                                salida = ToxUtils.EditID3(archivo, actions, tipo);
                                if (salida != null)
                                    richTextBox1.Text += salida + "\n\n";
                            }
                            break;
                    }
                }
                richTextBox1.Text = "FINISHED !!! \n__________\n\n\n" + richTextBox1.Text;
                ExitApp();
            }
            else
            {
                richTextBox1.Text = "No se encontraron archivos validos en la carpeta selecionada\n";
            }
            return exit;
        }

        public void ExitApp()
        {
            if (!debug)
            {
                Thread.Sleep(1000);
                this.Close();
                this.Dispose();
            }
        }

        public void hiloCambios()
        {
            while (!scanComplete) ;
            actions = OwnToSet(actions, textBox1.Text);
            ProcessFiles(files, actions);
            ExitApp();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Text = "Working ... ";
            this.Height = 100;
            button1.Enabled = false;
            Thread hilo = new Thread(hiloCambios);
            hilo.Start();
        }

        private Dictionary<string, string> OwnToSet(Dictionary<string, string> actions,String text)
        {
            Dictionary<string, string> exit = new Dictionary<string,string>();
            foreach (String llave in actions.Keys)
            {
                exit.Add(llave.Replace("own", "set"), text);
            }
            return exit;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
